﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Reports
{
	public partial class ProjectEditForm : CommonSenseControls.CommonSenseForm
	{
		private Project project = new Project();

		private SensePanelComboItem clientFilterComboBox;
		private SensePanelTextboxItem projectNameItem;
		private SensePanelTextboxItem projectTypeItem;
		private SensePanelTextboxItem projectNumberItem;


		public ProjectEditForm()
		{
			InitializeComponent();

			senseHeaderCtrl.Text = Resources.ProjectEditHeader;

			senseListCtrl.AddItem(new SensePanelDividerItem(string.Empty, Resources.ProjectEditExplanation));

			clientFilterComboBox = new SensePanelComboItem();
			clientFilterComboBox.LabelText = string.Format("{0}:", Resources.ClientLabel);
			Binder.BindCombobox(clientFilterComboBox.Items, Guid.Empty, Guid.Empty, true, false);
			senseListCtrl.AddItem(clientFilterComboBox);

			projectNameItem = new SensePanelTextboxItem();
			projectNameItem.LabelText = string.Format("{0}:", Resources.NameLabel);
			projectNameItem.Text = string.Empty;
			projectNameItem.MaxLength = DatabaseManager.MaxProjectNameLength;
			projectNameItem.ShowSeparator = true;
			senseListCtrl.AddItem(projectNameItem);

			projectTypeItem = new SensePanelTextboxItem();
			projectTypeItem.LabelText = string.Format("{0}:", Resources.TypeLabel);
			projectTypeItem.Text = string.Empty;
			projectTypeItem.MaxLength = DatabaseManager.MaxProjectTypeLength;
			projectTypeItem.ShowSeparator = true;
			senseListCtrl.AddItem(projectTypeItem);

			projectNumberItem = new SensePanelTextboxItem();
			projectNumberItem.LabelText = string.Format("{0}:", Resources.NumberLabel);
			projectNumberItem.Text = string.Empty;
			projectNumberItem.MaxLength = DatabaseManager.MaxProjectNumberLength;
			projectNumberItem.ShowSeparator = true;
			senseListCtrl.AddItem(projectNumberItem);

			SIP.Enable(senseListCtrl.Handle);
		}

		public Project Project
		{
			get { return project; }
			set { project = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			clientFilterComboBox.SelectedValue = project.Client;
			projectNameItem.Text = project.Name;
			projectTypeItem.Text = project.Type;
			projectNumberItem.Text = project.Number;
			base.OnLoad(e);
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				project.Client = (Guid)clientFilterComboBox.SelectedValue;
				project.Name = projectNameItem.Text;
				project.Type = projectTypeItem.Text;
				project.Number = projectNumberItem.Text;

				// check for propper selected client
				if (project.Client == DatabaseManager.AllClientsId)
				{
					if (SenseAPIs.SenseMessageBox.Show(Resources.DialogInvalidSelectedClient, Resources.DialogHeaderInvalidInput, SenseMessageBoxButtons.RetryCancel) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}

				// check for propper project name
				if (string.IsNullOrEmpty(project.Name))
				{
					if (SenseAPIs.SenseMessageBox.Show(Resources.DialogInvlidEmptyName, Resources.DialogHeaderInvalidInput, SenseMessageBoxButtons.RetryCancel) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}
			}

			base.OnClosing(e);
		}
	}
}